import { BASE_URL } from '@/config';
import { request } from '@/utils/request';
import type { Coupon, UseCouponRequest, RedeemCouponRequest } from '@/types/coupon';

/**
 * 优惠券相关接口
 * 职责:优惠券的查询、使用、兑换等操作
 */

// 获取用户所有优惠券
export function getUserCoupons(customerId: number) {
  return request<Coupon[]>({
    url: `${BASE_URL}/user/coupons/customer/${customerId}`,
    method: 'GET'
  });
}

// 根据状态获取优惠券
export function getCouponsByStatus(customerId: number, status: number) {
  return request<Coupon[]>({
    url: `${BASE_URL}/user/coupons/customer/${customerId}/status/${status}`,
    method: 'GET'
  });
}

// 获取优惠券详情
export function getCouponDetail(couponId: number) {
  return request<Coupon>({
    url: `${BASE_URL}/user/coupons/${couponId}`,
    method: 'GET'
  });
}

// 使用优惠券
export function useCoupon(data: UseCouponRequest) {
  return request({
    url: `${BASE_URL}/user/coupons/use`,
    method: 'POST',
    data
  });
}

// 兑换优惠券
export function redeemCoupon(customerId: number, data: RedeemCouponRequest) {
  return request<Coupon>({
    url: `${BASE_URL}/user/coupons/customer/${customerId}/redeem`,
    method: 'POST',
    data
  });
}

// 获取可用优惠券(用于下单时选择)
export function getAvailableCoupons(customerId: number, orderAmount: number) {
  return request<Coupon[]>({
    url: `${BASE_URL}/user/coupons/customer/${customerId}/available`,
    method: 'GET',
    params: { orderAmount }
  });
}